package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.service.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
public class CmsWebController extends BaseController{
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteCompanyService cmsSiteCompanyService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteDesignersService cmsSiteDesignersService;


    private CmsSiteInfo handPubData(Model model) {

        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteBanner cmsSiteBanner = new CmsSiteBanner();
        cmsSiteBanner.setModuleId((long)1);
        List<CmsSiteBanner> head = cmsSiteBannerService.selectCmsSiteBannerList(cmsSiteBanner);//获取头部信息
        cmsSiteBanner.setModuleId((long)2);
        List<CmsSiteBanner> footer = cmsSiteBannerService.selectCmsSiteBannerList(cmsSiteBanner);//获取尾部信息

        model.addAttribute("info", info);
        model.addAttribute("head", head);//进行封装
        System.out.println(JSON.toJSON(head));
        model.addAttribute("footer", footer);
        System.out.println(JSON.toJSON(footer));
        return info;
    }

    @GetMapping(value = {"/index", "/"})
    public String index(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "index";
    }

    @GetMapping("/zhuangxiu")
    public String zhuangxiu(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteCompany paramDesigners = new CmsSiteCompany();
            paramDesigners.setModuleId(m.getId());
            paramDesigners.setStatus(Constants.SUCCESS);
            List<CmsSiteCompany> designers =cmsSiteCompanyService.selectCmsSiteCompanyList(paramDesigners);
            designers.sort(Comparator.comparing(CmsSiteCompany::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("designers", designers);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "zhuangxiu";
    }


    @GetMapping("/shejishi")
    public String shejishi(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteDesigners paramDesigners = new CmsSiteDesigners();
            paramDesigners.setModuleId(m.getId());
            paramDesigners.setStatus(Constants.SUCCESS);
            List<CmsSiteDesigners> designers = cmsSiteDesignersService.selectCmsSiteDesignersList(paramDesigners);
            designers.sort(Comparator.comparing(CmsSiteDesigners::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("designers", designers);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "shejishi";
    }



    /**
     * 获取文章的分页信息
     *
     * @return*/


    @GetMapping("/cms_articles")
    @ResponseBody
    public TableDataInfo articlePage(CmsSiteDesigners param,@RequestParam(value = "pageSize",defaultValue = "4")Integer pageSize,
                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){

        PageUtils.startPage(pageNum,pageSize);
        PageUtils.orderBy("id asc");
        List<CmsSiteDesigners> data = cmsSiteDesignersService.selectCmsSiteDesignersList(param);
        TableDataInfo tableDataInfo = getDataTable(data);
        System.out.println(JSON.toJSON(tableDataInfo));
        return tableDataInfo;
    }

    @GetMapping("/cms_articles1")
    @ResponseBody
    public TableDataInfo articlePage1(CmsSiteDesigners param,@RequestParam(value = "pageSize",defaultValue = "4")Integer pageSize,
                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){

        PageUtils.startPage(pageNum,pageSize);
        PageUtils.orderBy("id asc");

        if(param.getB5()!=null){
            String b5 = param.getB5();
            param.setB5("%"+b5+"%");
        }
        if(param.getB3()!=null){
            String b3 = param.getB3();
            param.setB3("%"+b3+"%");
        }

        List<CmsSiteDesigners> data = cmsSiteDesignersService.selectCmsSiteDesignersLikeList(param);
        TableDataInfo tableDataInfo = getDataTable(data);
        System.out.println(JSON.toJSON(tableDataInfo));
        return tableDataInfo;
    }


    @GetMapping("/cms_zhuangxiu")
    @ResponseBody
    public TableDataInfo cms_zhuangxiu(CmsSiteCompany param,@RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize,
                                       @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        param.setModuleId((long)15);
        PageUtils.startPage(pageNum,pageSize);
        List<CmsSiteCompany> data = cmsSiteCompanyService.selectCmsSiteCompanyLikeList(param);
        TableDataInfo tableDataInfo = getDataTable(data);
        System.out.println(JSON.toJSON(tableDataInfo));
        return tableDataInfo;
    }

    @GetMapping("/effect")
    public String blog(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "effect";
    }

    @GetMapping("/fuwu")
    public String fuwu(Model model) {
         return "fuwu";
    }

}
